package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.CustomPopupWindow;
import com.jess.arms.widget.alertview.AlertView;
import com.jess.arms.widget.alertview.AlertViewOnItemClickListener;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.StringUtils;
import com.kmproject.haifu.di.component.DaggerHousePublishStorePublishInfoComponent;
import com.kmproject.haifu.mvp.contract.HousePublishStorePublishInfoContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.MessageEvent;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePublishResultBean;
import com.kmproject.haifu.mvp.model.entity.resbean.HousePublicBean;
import com.kmproject.haifu.mvp.presenter.HousePublishStorePublishInfoPresenter;
import com.kmproject.haifu.mvp.ui.widget.MultiSelectPopWindow;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 08/10/2019 12:10
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class HousePublishStorePublishInfoActivity extends BaseActivity<HousePublishStorePublishInfoPresenter> implements HousePublishStorePublishInfoContract.View {

    @BindView(R.id.toolbar_backImg)
    ImageView toolbarBackImg;
    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.back_right_icon)
    ImageView backRightIcon;
    @BindView(R.id.back_right_txt)
    TextView backRightTxt;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    AutoToolbar toolbar;
    @BindView(R.id.housepublishpublish_conten_dqzt_txt)
    TextView housepublishpublishContenDqztTxt;
    @BindView(R.id.housepublishpublish_conten_yjyhy_txt)
    TextView housepublishpublishContenYjyhyTxt;
    @BindView(R.id.housePublisInfo_edit)
    EditText housePublisInfoEdit;
    @BindView(R.id.housePublisInfo_txt_num)
    TextView housePublisInfoTxtNum;
    @BindView(R.id.HousePublishStorePublish_sure_txt)
    TextView HousePublishStorePublishSureTxt;
    @BindView(R.id.HousePublishOfficePublish_sure_lay)
    LinearLayout HousePublishOfficePublishSureLay;

    private List<HousePayWayBean> housedqzts,houseyjyhys;//当前状态,原经营行业
    private HousePayWayBean housedqzt,houseyjyhy;//选中的
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerHousePublishStorePublishInfoComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_house_publish_store_publish_info; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        toolbar.setBackgroundColor(ArmsUtils.getColor(this, R.color.color_EDEDED));
        housePublisInfoEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()<601)
                {
                    housePublisInfoTxtNum.setText(s.length()+"/800");
                }else {
                    housePublisInfoTxtNum.setText(s.toString().substring(0,800));
                }
            }
        });
    }

    @Override
    public void showLoading() {
    loadingDialog.show();
    }

    @Override
    public void hideLoading() {
    loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.housepublishpublish_conten_dqzt_txt, R.id.housepublishpublish_conten_yjyhy_txt, R.id.HousePublishStorePublish_sure_txt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.housepublishpublish_conten_dqzt_txt:
                mPresenter.getHousePayWayBean(Api.CURRENT_STATUS, "", housedqzts);

                break;
            case R.id.housepublishpublish_conten_yjyhy_txt:
                mPresenter.getHousePayWayBean(Api.ORIGINAL_BUSINESS, "", houseyjyhys);
                break;
            case R.id.HousePublishStorePublish_sure_txt:
                if (housedqzt==null||StringUtils.ViewisEnpty(housepublishpublishContenDqztTxt))
                {
                    showMessage("请选择当前状态");
                    return;
                }
                if (houseyjyhy==null||StringUtils.ViewisEnpty(housepublishpublishContenYjyhyTxt))
                {
                    showMessage("请选择原经营行业");
                    return;
                }
//                intent1.putExtra("selectModel", selectModel);//0表示商铺出租，1表示转租
//                intent1.putExtra("HousePublicBean", housePublicBean);
                HousePublicBean housePublicBean=(HousePublicBean)getIntent().getSerializableExtra("HousePublicBean");
                housePublicBean.setCurrentStatus(housedqzt.getDictId());
                housePublicBean.setOriginalBusiness(houseyjyhy.getDictId());
                housePublicBean.setDetail(housePublisInfoEdit.getText().toString());
                mPresenter.addHouseRent(AppUtil.entityToMap(housePublicBean));
                break;
        }
    }

    @Override
    public void addHouseRentSuccess() {
        CustomPopupWindow customPopupWindow=   AppUtil.showPopuWindow(this, CustomPopupWindow.inflateView(this, R.layout.custom_popuwindow_dialog),HousePublishStorePublishSureTxt);
        View view=customPopupWindow.getContentView();
        TextView membercenter_popuwindow_dialog_txt1=view.findViewById(R.id.membercenter_popuwindow_dialog_txt1);
        TextView membercenter_popuwindow_dialog_txt2=view.findViewById(R.id.membercenter_popuwindow_dialog_txt2);
        membercenter_popuwindow_dialog_txt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent();
                setResult(RESULT_OK,intent);
                killMyself();
            }
        });
        membercenter_popuwindow_dialog_txt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent();
                intent.putExtra("isGoToShow", 1);
                setResult(RESULT_OK,intent);
                killMyself();
            }
        });
    }

    @Override
    public void getHousePayWayBeanSuccess(List<HousePayWayBean> housePayWayBeans) {
        new MultiSelectPopWindow.Builder(this)
                .setConfirmListener(new MultiSelectPopWindow.OnConfirmClickListener() {
                    @Override
                    public void onClick(MessageEvent messageEvent) {
                        switch (housePayWayBeans.get(0).getValue()) {
                            case Api.CURRENT_STATUS:
                                //押金
                                housedqzts = housePayWayBeans;
                                housedqzt=housedqzts.get( messageEvent.getSelectId());
                                housepublishpublishContenDqztTxt.setText(messageEvent.getSelectData());
                                break;
                            case Api.ORIGINAL_BUSINESS:
                                //付款方式
                                houseyjyhys = housePayWayBeans;
                                houseyjyhy=houseyjyhys.get( messageEvent.getSelectId());
                                housepublishpublishContenYjyhyTxt.setText(messageEvent.getSelectData());
                                break;

                        }
                    }
                })
                .setDataArray(mPresenter.getHousePayWay(housePayWayBeans))
                .setCancel(ArmsUtils.getString(this, R.string.user_set_editinfo_cancel))
                .setTitle(getDialogTitle(housePayWayBeans.get(0).getValue()))
                .setConfirm(ArmsUtils.getString(this, R.string.sure_txt))
                .build()
                .show(HousePublishStorePublishSureTxt);
    }

    @Override
    public void updateHouseRentSuccess() {

    }

    /**
     * 根据类型动态设置标题
     *
     * @param value
     * @return
     */
    public String getDialogTitle(String value) {
        String temTitle = "";
        switch (value) {
            case Api.CURRENT_STATUS:
                //状态
                temTitle = ArmsUtils.getString(this, R.string.dilaog_titile_xzdqzt_txt);
                break;
            case Api.ORIGINAL_BUSINESS:
                //原经营行业
                temTitle = ArmsUtils.getString(this, R.string.dilaog_titile_xzyjyhy_txt);
                break;

        }
        return temTitle;
    }
}
